package edu.dublbo.app.habitplusfavorite.controller;

import edu.dublbo.app.habitplusfavorite.util.Constants;
import edu.dublbo.commons.lang.Result;
import edu.dublbo.commons.util.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;

@Slf4j
@RestController
@RequestMapping("/image")
public class ImageController {
    @Value("${logo-img.local-path}")
    private String logoLocPath;

    @GetMapping("/view-base64")
    public Result getBase64Img(String path) throws IOException {
        String res = "";
        if (path.startsWith(Constants.LOGO_REQUEST_PREFIX)) {
            String fileName = path.substring(Constants.LOGO_REQUEST_PREFIX.length() - 1);
            final byte[] bytes = IOUtil.toByteArray(
                    new FileInputStream(new File(logoLocPath + File.separator + fileName)));
            res = "data:image/png;base64, " + Base64.getEncoder().encodeToString(bytes);
        }
        return Result.success("", res);
    }

    @PostMapping("/upload")
    public Result uploadImg(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf('.'));
        fileName = UUIDUtil.uuid() + "_logo" + suffixName;

        File logoDir = new File(logoLocPath);
        boolean created = false;
        if (!logoDir.exists() || !logoDir.isDirectory())
            created = logoDir.mkdirs();
        if (created)
            log.info("目录不存在，但是已经创建了：{}", logoLocPath);

        String filePath = logoDir.getAbsolutePath() + File.separator + fileName;
        try {
            file.transferTo(new File(filePath));
            return Result.success("", Constants.LOGO_REQUEST_PREFIX + fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("文件写入失败");
        }

        /*try {
            File logoDir = new File(ResourceUtils.getURL("classpath:static/images/logos")
                    .getPath().
                    replace("%20", " ")
                    .replace('/', '\\'));
            log.info(logoDir.getAbsolutePath());
            log.info(logoDir.getPath());
            if (!logoDir.exists()) logoDir.mkdirs();
            String filePath = logoDir.getAbsolutePath() + File.separator + fileName;
            file.transferTo(new File(filePath));
            return Result.success("","/static/images/logos/" + fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            log.error("static 目录不存在！");
            return Result.fail("目录不存在");
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail();
        }*/
    }

}
